﻿using LibDll;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CDDY_VehicleRepo
{
    public partial class FrmVehicleInfo : Form
    {
        public FrmVehicleInfo(string strType, string strID)
        {
            InitializeComponent();
            if (strType == "View")
            {
                this.Text = "车型库详情-" + "查看";
                btnSave.Enabled = false;
            }
            else if (strType == "Update")
            {
                this.Text = "车型库详情-" + "修改";
                btnSave.Enabled = true;
            }
            if (strType == "Add")
            {
                this.Text = "车型库详情-" + "添加";
            }

            this.BindVehicleTypeInfo(strID);
        }

        private void BindVehicleTypeInfo(string strID)
        {
            try
            {
                var vConfig = Common.GetAppConfig();

                DataTable dt = new DataTable();

                string strSql = "select * from VehicleRepository where ID='" + strID + "'";

                DbHelper.GetTable(vConfig[0], strSql, ref dt);

                if(dt.Rows.Count>0)
                {
                    txtVehicleModel.Text = dt.Rows[0]["vehicleModel"].ToString().Trim();
                    txtSb.Text = dt.Rows[0]["sb"].ToString().Trim();
                    txtSccdz.Text = dt.Rows[0]["sccdz"].ToString().Trim();
                    txtSccmc.Text = dt.Rows[0]["sccmc"].ToString().Trim();
                    txtZkrs.Text = dt.Rows[0]["hdzkrs"].ToString();
                    string strBsxxs = dt.Rows[0]["bsxxs"].ToString();
                    for (int j = 0; j < cmbBsxxs.Items.Count; j++)
                    {
                        if (cmbBsxxs.Items[j].ToString().Contains(strBsxxs))
                        {
                            cmbBsxxs.SelectedIndex = j;
                            break;
                        }
                    }

                    string strQdfs = dt.Rows[0]["qdfs"].ToString();
                    for (int j = 0; j < cmbQdfs.Items.Count; j++)
                    {
                        if (cmbQdfs.Items[j].ToString().Contains(strQdfs))
                        {
                            cmbQdfs.SelectedIndex = j;
                            break;
                        }
                    }

                    string strQdzw = dt.Rows[0]["qdzw"].ToString();
                    if (strQdzw.Length > 0)
                    {
                        for (int i = 0; i < strQdzw.Length; i++)
                        {
                            string strQ = strQdzw.Substring(i, 1);

                            for (int j = 0; j < grpQDZW.Controls.Count; j++)
                            {
                                CheckBox chk = (CheckBox)grpQDZW.Controls[j];
                                if (strQ == chk.Text)
                                {
                                    chk.Checked = true;
                                }
                            }
                        }
                    }

                    txtCnzzxh.Text = dt.Rows[0]["cnzzxh"].ToString();
                    txtDcrl.Text = dt.Rows[0]["dcrl"].ToString();
                    txtZbzl.Text = dt.Rows[0]["zbzl"].ToString();
                    txtZzl.Text = dt.Rows[0]["zzl"].ToString();
                    txtPqgsl.Text = dt.Rows[0]["pqgsl"].ToString();
                    txtZj.Text = dt.Rows[0]["zj"].ToString();
                    string strPfjd = dt.Rows[0]["pfjd"].ToString();
                    for (int j = 0; j < cmbPFJD.Items.Count; j++)
                    {
                        if (cmbPFJD.Items[j].ToString().Contains(strPfjd))
                        {
                            cmbPFJD.SelectedIndex = j;
                            break;
                        }
                    }
                }

            }
            catch(Exception ex)
            {

            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string strVehicleModel = txtVehicleModel.Text.Trim();
                if (string.IsNullOrEmpty(strVehicleModel))
                {
                    MessageBox.Show("车辆型号不能为空!");
                    return;
                }

                string strSb = txtSb.Text.Trim();
                string strSccmc = txtSccmc.Text.Trim();
                string strSccdz = txtSccdz.Text.Trim();
                string strZkrs = txtZkrs.Text.Trim();
                string strBsxxs = cmbBsxxs.Text.Substring(0, 1);
                string strQdfs = cmbQdfs.Text.Substring(0, 3);
                string strQdzw = string.Empty;

                for (int i = 0; i < grpQDZW.Controls.Count; i++)
                {
                    CheckBox chk = (CheckBox)grpQDZW.Controls[i];
                    if (chk.Checked)
                    {
                        strQdzw += chk.Text;
                    }
                }

                string strCnzzxh = txtCnzzxh.Text.Trim();
                string strDcrl = txtDcrl.Text.Trim();
                string strZbzl = txtZbzl.Text.Trim();
                string strZzl = txtZzl.Text.Trim();
                string strPqgsl = txtPqgsl.Text.Trim();
                string strZj = txtZj.Text.Trim();
                string strPfjd = cmbPFJD.Text.Trim();

                VehicleRepository vehicleRepository = new VehicleRepository();
                vehicleRepository._bsxxs = strBsxxs;
                vehicleRepository._cnzzxh = strCnzzxh;
                vehicleRepository._dcrl = strDcrl;
                vehicleRepository._hdzkrs = strZkrs;
                vehicleRepository._pfjd = strPfjd;
                vehicleRepository._pqgsl = strPqgsl;
                vehicleRepository._qdfs = strQdfs;
                vehicleRepository._qdzw = strQdzw;
                vehicleRepository._sb = strSb;
                vehicleRepository._sccdz = strSccdz;
                vehicleRepository._sccmc = strSccmc;
                vehicleRepository._vehiclemodel = strVehicleModel;
                vehicleRepository._zbzl = strZbzl;
                vehicleRepository._zj = strZj;
                vehicleRepository._zzl = strZzl;

                var vRet = SaveVehicleRepository(vehicleRepository);
                if(vRet.IsSuccess)
                {
                    MessageBox.Show("保存成功！");
                    return;
                }
                else
                {
                    MessageBox.Show("保存失败," + vRet.Message);
                    return;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("保存失败," + ex.Message);
            }
        }


        /// <summary>
        /// 插入一条记录
        /// </summary>
        /// <param name="SaveVehicleRepository"></param>
        private ReturnInfo SaveVehicleRepository(VehicleRepository vehicleRepository)
        {
            ReturnInfo ret = new ReturnInfo();
            try
            {
                var vConfig = Common.GetAppConfig();
                string strSql = "select count(*) from VehicleRepository where vehiclemodel='" + vehicleRepository._vehiclemodel + "'";

                string strValue = DbHelper.GetValue(vConfig[0], strSql);

                if (Convert.ToInt32(strValue) == 0)
                {
                    strSql = " INSERT INTO[dbo].[VehicleRepository] ";
                    strSql += "(";
                    strSql += "[vehiclemodel]";
                    strSql += ",[sb]";
                    strSql += ",[sccmc]";
                    strSql += ",[sccdz]";
                    strSql += ",[hdzkrs]";
                    strSql += ",[bsxxs]";
                    strSql += ",[qdfs]";
                    strSql += ",[qdzw]";
                    strSql += ",[cnzzxh]";
                    strSql += ",[dcrl]";
                    strSql += ",[zbzl]";
                    strSql += ",[zzl]";
                    strSql += ",[zj]";
                    strSql += ",[pqgsl]";
                    strSql += ",[pfjd])";
                    strSql += "VALUES";
                    strSql += "(";
                    strSql += "'" + vehicleRepository._vehiclemodel + "',";
                    strSql += "'" + vehicleRepository._sb + "',";
                    strSql += "'" + vehicleRepository._sccmc + "',";
                    strSql += "'" + vehicleRepository._sccdz + "',";
                    strSql += "'" + vehicleRepository._hdzkrs + "',";
                    strSql += "'" + vehicleRepository._bsxxs + "',";
                    strSql += "'" + vehicleRepository._qdfs + "',";
                    strSql += "'" + vehicleRepository._qdzw + "',";
                    strSql += "'" + vehicleRepository._cnzzxh + "',";
                    strSql += "'" + vehicleRepository._dcrl + "',";
                    strSql += "'" + vehicleRepository._zbzl + "',";
                    strSql += "'" + vehicleRepository._zzl + "',";
                    strSql += "'" + vehicleRepository._zj + "',";
                    strSql += "'" + vehicleRepository._pqgsl + "',";
                    strSql += "'" + vehicleRepository._pfjd + "')";

                    var vRet = DbHelper.ExecuteInsertSql(vConfig[0], strSql);
                    if (vRet.IsSuccess == false)
                    {
                        ret.IsSuccess = false;
                        ret.Message = strSql;
                    }
                    else
                    {
                        ret.IsSuccess = true;
                    }
                }
                else
                {
                    strSql = "UPDATE [VehicleRepository]";
                    strSql += " SET ";
                    strSql += " [sb] ='" + vehicleRepository._sb + "',";
                    strSql += " [sccmc] ='" + vehicleRepository._sccmc + "',";
                    strSql += " [sccdz] ='" + vehicleRepository._sccdz + "',";
                    strSql += " [hdzkrs] ='" + vehicleRepository._hdzkrs + "',";
                    strSql += " [bsxxs] ='" + vehicleRepository._bsxxs + "',";
                    strSql += " [qdfs] ='" + vehicleRepository._qdfs + "',";
                    strSql += " [qdzw] ='" + vehicleRepository._qdzw + "',";
                    strSql += " [cnzzxh] ='" + vehicleRepository._cnzzxh + "',";
                    strSql += " [dcrl] ='" + vehicleRepository._dcrl + "',";
                    strSql += " [zbzl] ='" + vehicleRepository._zbzl + "',";
                    strSql += " [zzl] ='" + vehicleRepository._zzl + "',";
                    strSql += " [zj] ='" + vehicleRepository._zj + "',";
                    strSql += " [pqgsl] ='" + vehicleRepository._pqgsl + "',";
                    strSql += " [pfjd] ='" + vehicleRepository._pfjd + "' ";
                    strSql += " where vehiclemodel='" + vehicleRepository._vehiclemodel + "'";

                    var vRet = DbHelper.ExecuteInsertSql(vConfig[0], strSql);
                    if (vRet.IsSuccess == false)
                    {
                        ret.IsSuccess = false;
                        ret.Message = strSql;
                    }
                    else
                    {
                        ret.IsSuccess = true;
                    }
                }
            }
            catch (Exception ex)
            {
                ret.IsSuccess = false;
                ret.Message = ex.Message;
            }

            return ret;
        }
    }
}
